package cn.cnooc.controller;


import cn.hutool.core.collection.CollUtil;
import cn.cnooc.config.ResultInfo;
import cn.cnooc.service.HistoryDataService;
import cn.cnooc.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;


/**
 */
@RequestMapping("/photovoltaic")
@RestController
@CrossOrigin
public class WeatherController {

    @Autowired
    private WeatherService weatherService;
    @Autowired
    private HistoryDataService historyDataService;


    /**
     *  获取发电总量以及今日发电量和碳排放
     * @return
     */
    @GetMapping("/getCapacity")
    public ResultInfo getCapacity(){
      return weatherService.getCapacity();
    }


    /**
     *  获取热泵系统,储能SOC
     * @return
     */
    @GetMapping("/getHotWaterAndSoc")
    public ResultInfo getHotWaterAndSoc(){
        return weatherService.getHotWater();
    }



    /**
     *  获取设备总数
     * @return
     */
    @GetMapping("/getDeviceNumber")
    public ResultInfo getDeviceNumber(){
        return weatherService.getDeviceNumber();
    }


    /**
     *  查询并网点状态总览
     * @return
     */
    @GetMapping("/getCombinedStatus")
    public ResultInfo getCombinedStatus(){
        return weatherService.getCombinedStatus();
    }


    /**
     *  光优发电功率曲线
     * @return
     */
    @GetMapping("/getPhotovoltaicCurve")
    public Object getPhotovoltaicCurve(){
        Map<String, List> map1 = historyDataService.queryInstantaneousData("tb_donggu_inverter1", "yc51");
        Map<String, List> map2 = historyDataService.queryInstantaneousData("tb_donggu_inverter2", "yc51");
        List yData1 = map1.get("yData");
        List yData2 = map2.get("yData");
        List<String> newYdatad = CollUtil.newArrayList();
        IntStream.range(0, yData2.size()).forEach(i-> newYdatad.add(new BigDecimal(yData1.get(i).toString()).add(new BigDecimal(yData2.get(i).toString())).divide(BigDecimal.ONE,2, RoundingMode.CEILING).toString()));
        map1.put("yData", newYdatad);
        return map1;
    }


    /**
     *  并网功率曲线
     * @return
     */
    @GetMapping("/getGCPCCurve")
    public Object getGCPCCurve(){
        Map<String, List> map1 = historyDataService.queryInstantaneousData("tb_donggu_smartmeter", "yc10");
        List yData1 = map1.get("yData");
        List<String> newYdatad = CollUtil.newArrayList();
        yData1.forEach(s-> newYdatad.add(new BigDecimal(s.toString()).divide(new BigDecimal("1000"), 2, RoundingMode.CEILING).toString()));
        map1.put("yData", newYdatad);
        return map1;
    }

    /**
     *  交流负荷功率曲线
     * @return
     */
    @GetMapping("/getAcLoadCurve")
    public Object getAcLoadCurve(){
        Map<String, List> map1 = historyDataService.queryInstantaneousData("tb_donggu_smartswitch1", "yc33");
        List yData1 = map1.get("yData");
        List<String> newYdatad = CollUtil.newArrayList();
        yData1.forEach(s-> newYdatad.add(new BigDecimal(s.toString()).divide(new BigDecimal("1000"), 2, RoundingMode.CEILING).toString()));
        map1.put("yData", newYdatad);
        return map1;
    }

    /**
     *  中间大图
     * @return
     */
    @GetMapping("/getWiringDiagram")
    public ResultInfo getWiringDiagram(){
        return weatherService.getWiringDiagram();
    }
}
